/*
 *  Copyright (C) 2011 glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.components;

import java.awt.event.ActionEvent;
import java.util.logging.Level;
import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.JButton;
import org.balisunrise.application.Application;
import org.balisunrise.builder.FactoryException;
import org.balisunrise.common.Event;
import org.balisunrise.common.VisualAction;

/**
 * Ação Swing de um botão que é a representação visual de um VisualAction.
 * 
 * @author glauco
 */
public class ButtonVisualAction extends AbstractAction{

    private VisualAction<JButton> visualAction;

    public ButtonVisualAction(String name, Icon icon,
            VisualAction<JButton> visualAction) {

        super(name, icon);
        this.visualAction = visualAction;
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        Event evt = null;
        try{
            evt = Application.get().getFactory()
                    .createVisualActionEvent(visualAction, visualAction);
        }catch(FactoryException ex){
            Application.get().getLogger().log(Level.SEVERE,
                    "ButtonVisualAction não pode criar VisualActionEvent", ex);
        }

        visualAction.perform(evt);
    }

}
